﻿using System.Text;
using System.Text.Json;

public class AdbClient(string baseUrl)
{
    private readonly HttpClient _httpClient = new();
    private readonly string _baseUrl = baseUrl;

    public async Task<(bool success, string? message)> ConnectDevice(string deviceId)
    {
        var url = $"{_baseUrl}/devices/{deviceId}/connect";
        try
        {
            var response = await _httpClient.PostAsync(url, null);
            response.EnsureSuccessStatusCode();
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<Dictionary<string, object>>(content);
            return (true, result?["message"].ToString());
        }
        catch (HttpRequestException e)
        {
            return (false, e.Message);
        }
    }

    public async Task<(bool success, byte[]? screenshot, string message)> GetScreenshot(string deviceId)
    {
        var url = $"{_baseUrl}/devices/{deviceId}/screenshot";
        try
        {
            var response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode();
            var screenshotBytes = await response.Content.ReadAsByteArrayAsync();
            return (true, screenshotBytes, "");
        }
        catch (HttpRequestException e)
        {
            return (false, null, e.Message);
        }
    }

    public async Task<(bool success, string? message)> DisconnectDevice(string deviceId)
    {
        var url = $"{_baseUrl}/devices/{deviceId}/disconnect";
        try
        {
            var response = await _httpClient.PostAsync(url, null);
            response.EnsureSuccessStatusCode();
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<Dictionary<string, object>>(content);
            return (true, result?["message"].ToString());
        }
        catch (HttpRequestException e)
        {
            return (false, e.Message);
        }
    }

    public async Task<(bool success, string? message)> TapScreen(string deviceId, float x, float y)
    {
        var url = $"{_baseUrl}/devices/{deviceId}/tap";
        var jsonContent = JsonSerializer.Serialize(new { x, y });
        var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

        try
        {
            var response = await _httpClient.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<Dictionary<string, object>>(responseContent);
            return (true, result?["message"].ToString());
        }
        catch (HttpRequestException e)
        {
            return (false, e.Message);
        }
    }

    public async Task<(bool success, string? message)> SwipeScreen(string deviceId, float x1, float y1, float x2, float y2, int duration = 500)
    {
        var url = $"{_baseUrl}/devices/{deviceId}/swipe";
        var jsonContent = JsonSerializer.Serialize(new { x1, y1, x2, y2, duration });
        var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

        try
        {
            var response = await _httpClient.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<Dictionary<string, object>>(responseContent);
            return (true, result?["message"].ToString());
        }
        catch (HttpRequestException e)
        {
            return (false, e.Message);
        }
    }

    // public async Task<(bool success, List<DeviceInfo>? devices, string message)> ListConnectedDevices()
    // {
    //     var url = $"{_baseUrl}/devices";
    //     try
    //     {
    //         var response = await _httpClient.GetAsync(url);
    //         response.EnsureSuccessStatusCode();
    //         var content = await response.Content.ReadAsStringAsync();
    //         var result = JsonSerializer.Deserialize<DeviceListResponse>(content);
    //         return (true, result?.Devices, "");
    //     }
    //     catch (HttpRequestException e)
    //     {
    //         return (false, null, e.Message);
    //     }
    // }
}

// public class DeviceListResponse
// {
//     public List<DeviceInfo>? Devices { get; set; }
// }

// public class DeviceInfo
// {
//     public string? Id { get; set; }
//     public string? Status { get; set; }
//     public bool ManagedByService { get; set; }
// }